<template>
  <view class="integral">
    <navbarIntegral :styleData="styleData" ref="integral"/>

    <scroll-view class="scroll" scroll-y>
      <!-- 占位 -->
      <view class="placeholder" />

      <view class="task">
        <view class="title-task">积分任务</view>

        <view class="task-item">
          <view class="item-Box" v-for="item in taskList">
            <view class="l">
              <view class="icon">
                <image class="" style="height: 64rpx; width: 64rpx" :src="item.icon"
                  mode="aspectFit|aspectFill|widthFix" />
              </view>

              <text class="name">
                {{ item.taskName }}
              </text>

              <text class="" selectable="false" space="false" decode="false">
                +{{ item.integralQuantity }}
              </text>
            </view>

            <view @click="handClick(item)" :class="item.status === '0' || item.status === '1' ? 'r' : 'rr'">
              <text class="text" selectable="false" space="false" decode="false">
                {{ getStatusText(item.status) }}
              </text>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import navbarIntegral from "./components/navbarIntegral.vue";
import { getIntegralTaskListAPI, toSignAPI } from "@/api/subPage/mine";
export default {
  components: { navbarIntegral },
  computed: {
    statusText() {
      return {
        0: "未完成",
        1: "未签到",
        2: "已完成",
      };
    },
  },
  data() {
    return {
      styleData: {
        text: "我的积分",
        color: "#ffffff",
      },

      taskList: [],

      iconList: [
        {
          icon: require("@/static/images/mIntergral/mr.png"),
        },
        {
          icon: require("@/static/images/mIntergral/ws.png"),
        },
        {
          icon: require("@/static/images/mIntergral/sc.png"),
        },
        {
          icon: require("@/static/images/mIntergral/yq.png"),
        },
      ],
    };
  },

  // 监控data中的数据变化
  watch: {},
  onLoad(options) {
    this.getList();
  },
  onShow() { },
  onReady() { },
  methods: {
    async handClick(item) {
      if (item.status === '2') return;
      const taskName = item.taskName;
      switch (taskName) {
        case "每日签到":
          const res = await toSignAPI();
          if (res.code === 200) {
            uni.showToast({
              icon: "none",
              title: "签到成功",
            });
          }
          break;

        case "完善企业资料":
          uni.navigateTo({
            url: "/subPage/enterprise/mine/details/enterpriseData",
          });
          break;
        case "首次邀请投递":
          uni.switchTab({
            url: "/pages/tabbarChange/sencondPage/index",
          });
          break;

        case "首次发布岗位":
          uni.switchTab({
            url: "/pages/tabbarChange/thirdlyPage/index",
          });
          break;

        default:
          break;
      }
      this.getList()
      this.$refs.integral.getSIgn()
    },

    async getList() {
      const { data, code } = await getIntegralTaskListAPI();
      if (code === 200) {
        this.taskList = data.map((item, index) => ({
          ...item,
          icon: this.iconList[index % this.iconList.length].icon,
        }));
      }
    },

    getStatusText(status) {
      return this.statusText[status] || "未知状态";
    },
  },
};
</script>
<style lang="scss" scoped>
.integral {
  height: 100vh;
  background-color: #f9f9f9;
  overflow: hidden;

  .scroll {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 0 24rpx;
    overflow: hidden;

    .placeholder {
      height: 415rpx;
    }

    .task {
      display: flex;
      flex-direction: column;
      width: 100%;

      .title-task {
        width: 144rpx;
        height: 50rpx;
        font-family: PingFangSC-Medium;
        font-weight: 500;
        font-size: 34rpx;
        color: #000000e0;
        letter-spacing: 0;
        margin-top: 10rpx;
        margin-bottom: 32rpx;
      }

      .task-item {
        width: 702rpx;

        display: flex;
        flex-direction: column;

        .item-Box {
          height: 142rpx;
          display: flex;
          justify-content: space-between;
          align-items: center;
          background: #ffffff;
          box-shadow: 0 0 12rpx 0 #e2e2e299;
          border-radius: 16rpx;
          padding: 0 24rpx;
          margin-bottom: 24rpx;

          .l {
            display: flex;
            align-items: center;

            .icon {
              width: 64rpx;
              height: 64rpx;
              margin-right: 16rpx;
            }

            .name {
              height: 45rpx;
              font-weight: 600;
              font-size: 32rpx;
              color: #000000e0;
              letter-spacing: 0;
            }
          }

          .r {
            width: 170rpx;
            height: 64rpx;
            background: #ff9e24;
            border-radius: 44rpx;
            display: flex;
            justify-content: center;
            align-items: center;

            .text {
              color: #ffffff;
              font-size: 24rpx;
              font-weight: 500;
            }
          }

          .rr {
            width: 170rpx;
            height: 64rpx;
            background: #d8d8d83d;
            border-radius: 44rpx;
            display: flex;
            justify-content: center;
            align-items: center;

            .text {
              font-weight: 500;
              font-size: 24rpx;
              color: #000000e0;
            }
          }
        }
      }
    }
  }
}
</style>
